﻿using IDAL;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
	public class StaticBaseDAL<T> : IBaseDAL<T> where T : class, new()
	{
		CodeFirstTestMvcDBEntities db = new CodeFirstTestMvcDBEntities();
		public T AddDonator(T entity)
		{
			return db.Set<T>().Add(entity);
		}

		public bool DeleteEntity(T entity)
		{
			db.Entry<T>(entity).State = System.Data.Entity.EntityState.Deleted;
			//return db.SaveChanges() > 0;
			return true;
		}

		public IQueryable<T> LoadEntities(System.Linq.Expressions.Expression<Func<T, bool>> whereLambda)
		{
			return db.Set<T>().Where(whereLambda);
		}

		public IQueryable<T> LoadPageEntities<s>(int pageIndex, int pageSize, out int totalCount, System.Linq.Expressions.Expression<Func<T, bool>> whereLambda, System.Linq.Expressions.Expression<Func<T, s>> orderbyLambda, bool isAsc)
		{
			var temp = db.Set<T>().Where<T>(whereLambda);
			totalCount = temp.Count();
			if (isAsc)
			{
				temp = temp.OrderBy<T, s>(orderbyLambda).Skip<T>((pageIndex - 1) * pageSize).Take<T>(pageSize);
			}
			else
			{
				temp = temp.OrderByDescending<T, s>(orderbyLambda).Skip<T>((pageIndex - 1) * pageSize).Take<T>(pageSize);
			}
			return temp;
		}

		public bool UpdateEntity(T entity)
		{
			db.Entry<T>(entity).State = System.Data.Entity.EntityState.Modified;
			//return db.SaveChanges() > 0;
			return true;
		}
	}
}
